#include "widget.h"
#include "ui_widget.h"
#include <QGridLayout>
#include <QPushButton>
#include <QSizePolicy>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QGridLayout* layout = new QGridLayout;
    this->setLayout(layout);

    QPushButton* b1 = new QPushButton("按钮1");
    QPushButton* b2 = new QPushButton("按钮2");
    QPushButton* b3 = new QPushButton("按钮3");
    QPushButton* b4 = new QPushButton("按钮4");
    QPushButton* b5 = new QPushButton("按钮5");
    QPushButton* b6 = new QPushButton("按钮6");

    layout->addWidget(b1,0,0);
    layout->addWidget(b2,0,1);
    layout->addWidget(b5,0,2);
    layout->addWidget(b3,1,0);
    layout->addWidget(b4,1,1);
    layout->addWidget(b6,1,2);
    // 设置拉伸比例
    // 第0列占据 1 / 6
    layout->setColumnStretch(0,1);
    // 第1列占据 2 / 6
    layout->setColumnStretch(1,2);
    // 第2列占据 3 / 6
    layout->setColumnStretch(2,3);

    // 设置行拉伸系数，按钮的行拉伸系数固定，需要更改属性 sizePolicy。
    // 设置按钮属性的sizePolicy,此时的按钮水平方向和垂直方向都会舒展开
    b1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    b2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    b3->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    b4->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    b5->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    b6->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    layout->setRowStretch(0,1);
    // 拉伸系数为0，即固定大小不参与拉伸
    layout->setRowStretch(1,2);


//    layout->addWidget(b1,0,0);
//    layout->addWidget(b2,0,1);
//    layout->addWidget(b3,0,2);
//    layout->addWidget(b4,0,3);

//    layout->addWidget(b1,0,0);
//    layout->addWidget(b2,1,0);
//    layout->addWidget(b3,2,0);
//    layout->addWidget(b4,9,0);
}

Widget::~Widget()
{
    delete ui;
}

